package com.wf.smartcomm.server.admin.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.aspectj.util.FileUtil;
import org.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.Image;
import com.wf.smartcomm.server.bean.Shop;
import com.wf.smartcomm.server.bean.ThumbImage;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.service.ImageService;
import com.wf.smartcomm.server.service.ShopService;
import com.wf.smartcomm.server.service.ThumbImageService;
import com.wf.smartcomm.server.util.IdUtils;
import com.wf.smartcomm.server.util.ImageUtils;
import com.wf.smartcomm.server.util.PushUtils;
import com.wf.smartcomm.server.util.StringUtil;

import antlr.StringUtils;

@Controller("adminShopAction")
@Scope("prototype")
public class ShopAction extends BaseAction implements ModelDriven<Shop>{
	
	private List<File> upload;  
    // /多个上传文件的类型集合  
    private List<String> uploadContentType;  
   // 多个上传文件的文件名集合  
    private List<String> uploadFileName;
    
    private Shop shop = new Shop();
    
    @Resource
    private ShopService shopServiceImpl;
    
	public List<File> getUpload() {
		return upload;
	}
	public void setUpload(List<File> upload) {
		this.upload = upload;
	}
	public List<String> getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public List<String> getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	
	public void save() throws Exception{
		User user = (User) session.getAttribute("loginUser");
		shop.setUser(user);
		shop.setVillage(user.getLoginVillage());
		
		
		if (upload != null && upload.size() > 0) {
			/*List<Image> images = new ArrayList<Image>();
			List<ThumbImage> thumbs = new ArrayList<ThumbImage>();*/
			for (File file : upload) {
				String fileName = IdUtils.uuid()+".png";
				//Image image = new Image();
				String path = request.getServletContext().getRealPath(
						"images");
				//String url = request.getRequestURL().substring(0, request.getRequestURL().lastIndexOf("jsp"))+"images/";
				String url = "http://"+PushUtils.pushConfig.getProperty("ImageServerIp");
				if (request.getServerPort() != 80) {
					url+=":"+request.getServerPort();
				}
				url+=request.getContextPath()+"/images/";
				/*image.setUrl(url + fileName);
				image.setFileName(fileName);*/
				shop.setImageUrl(url+fileName);
				File des = new File(path + File.separator + fileName);
				FileUtil.copyFile(file, des);
				/*ThumbImage thumbImage = new ThumbImage();
				thumbImage.setFileName(fileName);
				thumbImage.setShop(shop);
				thumbImage.setUrl(url.replace("images", "thumbImages") +fileName);*/
				ImageUtils.getThumbnail(des.getAbsolutePath(),
						request.getServletContext().getRealPath(
								"thumbImages")+File.separator+fileName, 150, 100);
				/*images.add(image);
				thumbs.add(thumbImage);*/
			}
			/*imageServiceImpl.save(images);
			thumbServiceImpl.save(thumbs);*/
		}
		
		if (!StringUtil.isEmpty(shop.getObjectId())) {
			if (StringUtil.isEmpty(shop.getImageUrl())) {
				shop.setImageUrl(shopServiceImpl.get(shop.getObjectId()).getImageUrl());
			}
			shop.setCreatedAt(String.valueOf(System.currentTimeMillis()));
			shopServiceImpl.update(shop);
			//thumbServiceImpl.deleteShopImages(shop.getObjectId());
		} else {
			shopServiceImpl.save(shop);
		}
		printJsonData(200);
	}
	
	public void list() throws Exception {
		String aoData = request.getParameter("aoData");
		String sEcho = "";
		String iDisplayStart = "";
		String iDisplayLength = "";
		String sSearch = "";
		
		JSONObject jo = new JSONObject(aoData);
		sEcho = jo.getString("sEcho");
		iDisplayStart = jo.getString("iDisplayStart");
		iDisplayLength = jo.getString("iDisplayLength");
		sSearch = jo.getString("sSearch");
		
		User user = (User) session.getAttribute("loginUser");
		page.setBeginIndex(Integer.parseInt(iDisplayStart));
		page.setPageSize(Integer.parseInt(iDisplayLength));
		if (!StringUtil.isEmpty(sSearch)) {
			String type = "";
			if (sSearch.contains("餐") || sSearch.contains("饮")) {
				type = "1";
			} else if(sSearch.contains("服") || sSearch.contains("装")){
				type = "2";
			}else if(sSearch.contains("娱") || sSearch.contains("乐")){
				type = "3";
			}else if(sSearch.contains("母") || sSearch.contains("婴")){
				type = "4";
			}else if(sSearch.contains("美") || sSearch.contains("容")){
				type = "5";
			}else if(sSearch.contains("数") || sSearch.contains("码")){
				type = "6";	
			}else if(sSearch.contains("百") || sSearch.contains("货")){
				type = "7";
			}
			
			if (!StringUtil.isEmpty(type)) {
				shopServiceImpl.listByType(user.getLoginVillage(),type,page);
			}else {
				shopServiceImpl.query(user.getLoginVillage(),sSearch, page);
			}
			
		}else {
			shopServiceImpl.listByVillage(user.getLoginVillage(), page);
		}
		
		int  initEcho = Integer.parseInt(sEcho)+1;
		int count = page.getTotalCount();
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("iTotalRecords", count);
		dataMap.put("sEcho",initEcho);
		dataMap.put("iTotalDisplayRecords", count);
		dataMap.put("aaData", page.getDataList());
		printJsonData(outGson.toJson(dataMap));
	}
	
	public void delete() throws Exception{
		shopServiceImpl.delete(shop);
		printJsonData(200);
	}
	
	public String edit() throws Exception{
		shop = shopServiceImpl.get(shop.getObjectId());
		request.setAttribute("shop", shop);
		return "edit";
	}
	
	@Override
	public Shop getModel() {
		return shop;
	}  
}
